package com.jsbb.config;

import com.jsbb.interceptor.AuthorityInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

/**
 * Web配置
 *
 * @author w
 * @date 2021-03-05
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * SpringBoot2开始静态资源默认不能直接访问，需要加此配置文件，或者在配置文件中添加如下配置：
     * spring.mvc.static-path-pattern=/**
     * spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * token拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthorityInterceptor()).addPathPatterns("/**").excludePathPatterns("/favicon.ico", "/error", "/", "/*/*/index", "/login", "/static/**", "/webjars/**", "/swagger**", "/doc.html", "/v2/api-docs");
    }

    /**
     * 支持URL带转义字符
     *
     * @param pathMatchConfigurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer pathMatchConfigurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setUrlDecode(false);
        pathMatchConfigurer.setUrlPathHelper(urlPathHelper);
    }
}
